package org.example.leetcode.codeExecutor.utils;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;

import javax.ws.rs.core.MultivaluedMap;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class CodeExecutorUtils {
    public static void main(String[] args) throws InterruptedException {
        DockerClient dockerClient= DockerClientBuilder.getInstance("tcp://47.101.182.243:2375").build();
//        dockerClient.pullImageCmd("zhc").exec(new PullImageResultCallback()).awaitCompletion();

//        CreateContainerCmd createContainerCmd=dockerClient.createContainerCmd("zhc");
//
//        HostConfig hostConfig = new HostConfig();
//        //限制内存
//        hostConfig.withMemory(100 * 1000 * 1000L);
//        hostConfig.setBinds(new Bind("/app/myapp",new Volume("/root/app")));
//        CreateContainerResponse container = createContainerCmd
//                .withHostConfig(hostConfig)
//                .exec();
//        String id = container.getId();
//        System.out.println(id);
//        dockerClient.startContainerCmd(id).exec();
//        System.out.println("容器启动");


//        System.out.println(exec);
        ExecStartResultCallback execStartResultCallback = new ExecStartResultCallback(){

            /**
             * 该方法为程序执行完命令后会调用的方法
             */
            @Override
            public void onComplete() {
                System.out.println("123");
            }

            @Override
            public void onNext(Frame frame) {
                StreamType streamType = frame.getStreamType();
                if (StreamType.STDERR.equals(streamType)){
                    System.out.println("输出错误结果: " + new String(frame.getPayload()));

                }else {
                    System.out.println("输出结果: " + new String(frame.getPayload()));
                }
                super.onNext(frame);
            }
        };
        dockerClient.startContainerCmd("b3e6f8b0fdd0").exec();
//        String[] cmdArray= new String[]{"gcc","/app"};
        InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd("b3e6f8b0fdd0").exec();
        System.out.println(inspectContainerResponse.getState().getStatus());
        ExecCreateCmdResponse exec = dockerClient.execCreateCmd("b3e6f8b0fdd0").withCmd("command").exec();
        dockerClient.execStartCmd("b3e6f8b0fdd0").exec(execStartResultCallback).awaitCompletion();


//        dockerClient.removeContainerCmd("723aca3d5382").exec();
    }
}
